package com.yc.testPro;

import java.io.IOException;
import java.util.logging.ConsoleHandler;
import java.util.logging.FileHandler;
import java.util.logging.Formatter;
import java.util.logging.Level;
import java.util.logging.LogRecord;
import java.util.logging.Logger;

public class TestTomcat {
	
	/**
	 * 测试jdk log
	 */
	public void testLog() {
		Logger log = Logger.getLogger("lavasoft");
		log.setLevel(Level.INFO);
		
		Logger log1 = Logger.getLogger("lavasoft.bl");
		System.out.println(log==log1);
		
		Logger log2 = Logger.getLogger("lavasoft.blog");
//		log2.setLevel(Level.WARNING);
		
		log.info("aaa");
		log.info("1111");
		log2.info("bbb");
		log2.fine("fine");
	}
	
	/**
	 * Handler负责把日志输出到各种介质
	 * @throws IOException 
	 * @throws SecurityException 
	 */
	public void testHandler() throws SecurityException, IOException {
		Logger log = Logger.getLogger("lavasoft");
		log.setLevel(Level.INFO);
		Logger log2 = Logger.getLogger("lavasoft.blog");
		
		ConsoleHandler consoleHandle = new ConsoleHandler();
		consoleHandle.setLevel(Level.ALL);
		log.addHandler(consoleHandle);
		
		FileHandler fileHandler = new FileHandler("E:/temp/testlog%g.log");
		fileHandler.setLevel(Level.INFO);
		fileHandler.setFormatter(new MyLogHander());
		
		log.addHandler(fileHandler);
		log.info("aaa");
		
		log2.info("bbb");
		log2.fine("fine");
		
	}
	
	/**
	 * 格式化日志
	 * @author yuancan
	 *
	 */
	class MyLogHander extends Formatter {

		@Override
		public String format(LogRecord record) {
			
			return record.getLevel() + ":" + record.getMessage() + "\n";
		}
		
	}
	
	

	public static void main(String[] args) throws SecurityException, IOException {
		TestTomcat tt = new TestTomcat();
//		tt.testLog();
//		tt.testHandler();

	}

}
